package com.github.freewu.commons.libs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.lqkj.commons.R;
import com.github.freewu.commons.libs.LoadingView;


/**
 * Created by yb on 2016/4/19.
 */
public class CustomProgressDialog extends Dialog {
    private Context context = null;
    private static CustomProgressDialog customProgressDialog = null;

    public CustomProgressDialog(Context context){
        super(context);
        this.context = context;
    }
    public void setActivityNull(){
        this.context=null;
        customProgressDialog = null;
    }

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    public static void disMissDialog(){
        if(customProgressDialog!=null){
            if(customProgressDialog.isShowing()){
                LoadingView loadingView = (LoadingView) customProgressDialog.findViewById(R.id.loadingView);
                loadingView.cancel();
                customProgressDialog.dismiss();
                customProgressDialog.setActivityNull();
            }
        }
    }

    public static void createDialog(Activity context, String strMessage){
        if (customProgressDialog==null) {
            customProgressDialog = new CustomProgressDialog(context, R.style.CustomProgressDialog);
        }else{
            if (customProgressDialog.isShowing()){
                customProgressDialog.cancel();
            }
            customProgressDialog = new CustomProgressDialog(context, R.style.CustomProgressDialog);
        }
        customProgressDialog.setCanceledOnTouchOutside(false);
        customProgressDialog.setContentView(R.layout.loading_dialog);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        LoadingView loadingView = (LoadingView) customProgressDialog.findViewById(R.id.loadingView);
        TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.tipTextView);
        if (tvMsg != null) {
            tvMsg.setText(strMessage);
        }
        // 使用ImageView显示动画
        loadingView.start();

        customProgressDialog.show();
    }
}
